package com.example.actordemo1.bus;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.example.actordemo1.tools.FutureActor;

public class EventStreamActor extends AbstractActor  {
    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(
                        Jazz.class,
                        msg -> System.out.printf(getSelf().path()+"%s is listening to: %s%n", getSelf().path().name(), msg))
                .match(
                        Electronic.class,
                        msg -> System.out.printf("%s is listening to: %s%n", getSelf().path().name(), msg))
                .build();
    }

    public static void main(String[] args) {
        ActorSystem actorSystem = ActorSystem.create("sys");
//        ActorRef actorRef = actorSystem.actorOf(Props.create(EventStreamActor.class), "eventStreamActor");

        final ActorRef jazzListener = actorSystem.actorOf(Props.create(EventStreamActor.class));
        final ActorRef musicListener = actorSystem.actorOf(Props.create(EventStreamActor.class));
        actorSystem.eventStream().subscribe(jazzListener, Jazz.class);
        actorSystem.eventStream().subscribe(musicListener, AllKindsOfMusic.class);


        actorSystem.eventStream().publish(new Electronic("Parov Stelar"));

        // jazzListener and musicListener will be notified about Jazz:
//        actorSystem.getEventStream().publish(new Jazz("Sonny Rollins"));


    }
}
